package com.eascs.web.o2o.scheduler.task;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.service.IRabbitMsgRecordService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.web.o2o.scheduler.service.SynchroWorkflow;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTaskNode;
import com.eascs.workflow.common.service.timeEffective.AgingDubboService;
import com.eascs.workflow.common.service.timeEffective.TimeffectiveDubboService;
import com.eascs.workflow.common.service.wf.WorkFlowService;
import com.eascs.workflow.common.vo.ModelResult;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.util.List;

/**
      workflow：与workflow相关的定时任务，并且数据落库在workflow中
 */
@Component("workFlowTask")
public class WorkFlowSynchroTask {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Reference
	WorkFlowService workFlowService;

	@Reference
	TimeffectiveDubboService timeffective;

	@Reference
	AgingDubboService agingDubboService;

	@Inject
	SynchroWorkflow		synchroWorkflow;

	@Reference
	IRabbitMsgRecordService mqService;

	/**
	 * workflow 流程节点超时8小时提醒
	 * 周一到周五每天上午9点半，下午5点半发短信
	 */
	@Scheduled(cron = "0 30 9,17 ? * MON-FRI")
	public void wfWorkTimeout8Sms() {
		logger.info("[workflow] [流程节点超时8小时提醒]任务[开始]......");

		StopWatch stopWatch = new StopWatch();
		ModelResult<Boolean> result = workFlowService.wfWorkTimeout8Sms();
		long stop = stopWatch.elapsedTime() / 60000;

		logger.info("[workflow] [流程节点超时8小时提醒]任务[结束]......");
	}

	/**
	 * workflow 流程节点超时16小时提醒
	 * 周一到周五每天上午9点半，下午5点半发短信
	 */
	@Scheduled(cron = "0 30 9,17 ? * MON-FRI")
	public void wfWorkTimeout16Sms() {
		logger.info("[workflow] [流程节点超时16小时提醒]任务[开始]......");

		StopWatch stopWatch = new StopWatch();
		ModelResult<Boolean> result = workFlowService.wfWorkTimeout16Sms();
		long stop = stopWatch.elapsedTime() / 60000;

		logger.info("[workflow] [流程节点超时16小时提醒]任务[结束]......");
	}
	
	/**
	 * workflow 待办提醒任务 每天一次 2016年9月13日
	 */
	@Scheduled(cron = "0 0 4 * * ?")
	public void wfWorkWarnEmail() {
		logger.info("[workflow] [待办提醒]任务[开始]......");

		StopWatch stopWatch = new StopWatch();
		ModelResult<Boolean> result = workFlowService.wfWorkWarnEmail();
		long stop = stopWatch.elapsedTime() / 60000;
		if (result.isSuccess()) {
			boolean flag = result.getModel();
			if (flag) {
				logger.info("[workflow] [待办提醒]任务[完成]，共耗时：[{}]分", stop);
			} else {
				logger.warn("[workflow] [待办提醒]任务[失败]，共耗时：[{}]分", stop);
			}
		} else {
			logger.warn("[workflow] [待办提醒]任务[异常]，错误信息[{}],共耗时：[{}]分", result.getErrMsg(), stop);
		}

		logger.info("[workflow] [待办提醒]任务[结束]......");
	}

	/**
	 * 时效管理0
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void timeffective() {
		logger.info("[workflow] [时效管理零点]任务[开始]......");
		int pageSize = 50;
		int pageIndex = 0;
		StopWatch time = new StopWatch();

		while (pageIndex >= 0) {
			ModelResult<List<ProcessTaskNode>> result = timeffective.queryProcessNodeByPage(pageSize, pageIndex);
			if (result.isSuccess()) {
				List<ProcessTaskNode> tasks = result.getModel();
				if (CollectionUtils.isEmpty(tasks)) {
					break;
				}

				logger.info("[workflow] [时效管理零点]任务第[{}]页[ProcessTaskNode]共有数据[{}]条", pageIndex, tasks.size());
				for (ProcessTaskNode task : tasks) {
					ModelResult<Boolean> result2 = timeffective.timeffectiveNodeTask(task);
					if (!result2.isSuccess()) {
						logger.error("[workflow] [时效管理零点]任务[错误][timeffective.timeffectiveTask]......pageIndex:[{}]", pageIndex);
					}
				}

				pageIndex++;
				if (tasks.size() < pageSize) {
					pageIndex = -1;
				}
			} else {
				logger.error("[workflow] [时效管理零点]任务[错误]......pageIndex:[{}],ErrorMsg:[{}]", result.getErrMsg(), pageIndex);
			}
		}
		logger.info("[workflow] [时效管理零点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}

	/**
	 * 时效管理2
	 */
	@Scheduled(cron = "0 0 2 * * ?")
	public void timeffective2() {
		logger.info("[workflow] [时效管理2点]任务[开始]......");
		int pageSize = 50;
		int pageIndex = 0;
		StopWatch time = new StopWatch();

		while (pageIndex >= 0) {
			ModelResult<List<ProcessTaskMain>> result = timeffective.queryProcessMainByPage(pageSize, pageIndex);

			if (result.isSuccess()) {
				List<ProcessTaskMain> tasks = result.getModel();
				if (CollectionUtils.isEmpty(tasks)) {
					break;
				}

				logger.info("[workflow] [时效管理2点]任务第[{}]页[ProcessTaskMain]共有数据[{}]条", pageIndex, tasks.size());
				for (ProcessTaskMain task : tasks) {
					ModelResult<Boolean> result2 = timeffective.timeffectiveMainTask(task);
					if (!result2.isSuccess()) {
						logger.error("[workflow] [时效管理2点]任务[错误][timeffective.timeffectiveMainTask]......pageIndex:[{}]", pageIndex);
					}
				}

				pageIndex++;
				if (tasks.size() < pageSize) {
					pageIndex = -1;
				}
			} else {
				logger.error("[workflow] [时效管理2点]任务[错误]......pageIndex:[{}]ErrorMsg:[{}]", result.getErrMsg(), pageIndex);
			}
		}
		logger.info("[workflow] [时效管理2点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}
	//时效管理  3
	@Scheduled(cron = "0 0 6 * * ?")
	public void timeffective3() {
			logger.info("[workflow] [同步贷款单6点]任务[开始]......");
			StopWatch time = new StopWatch();
				try {
					ModelResult<Boolean> result = agingDubboService.insertLoanapply();
					logger.info("接口返回对象：[{}]",result.getModel());
				} catch (Exception e) {
					logger.info("异常原因：[{}]", e.getMessage());
				}
			logger.info("[workflow][同步贷款单6点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
		}
	//时效管理  4
	@Scheduled(cron = "0 20 6 * * ?")
	public void timeffective4() {
			logger.info("[workflow] [同步主流程6点20分]任务[开始]......");
			StopWatch time = new StopWatch();
				try {
					ModelResult<Boolean> result = agingDubboService.insertWorkFlow();
					logger.info("接口返回对象：[{}]",result.getModel());
				} catch (Exception e) {
					logger.info("异常原因：[{}]", e.getMessage());
				}
			logger.info("[workflow][同步贷款单6点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
		}
	//时效管理  5
	@Scheduled(cron = "0 40 6 * * ?")
	public void timeffective5() {
			logger.info("[workflow] [同步节点6点40分]任务[开始]......");
			StopWatch time = new StopWatch();
				try {
					ModelResult<Boolean> result = agingDubboService.insertNode();
					logger.info("接口返回对象：[{}]",result.getModel());
				} catch (Exception e) {
					logger.info("异常原因：[{}]", e.getMessage());
				}
			logger.info("[workflow][同步节点6点40分]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
		}
	//时效导出
	@Scheduled(cron = "0 0 4 * * ?")
	public void timeffective6() {
			logger.info("[workflow] [时效导出4点]任务[开始]......");
			StopWatch time = new StopWatch();
				try {
					ModelResult<Boolean> result = agingDubboService.syncAgingExport();
					logger.info("时效导出接口返回对象：[{}]",result.getModel());
				} catch (Exception e) {
					logger.info("时效导出异常原因：[{}]", e.getMessage());
				}
			logger.info("[workflow][时效导出4点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
		}
	
	
	/**
	 *将oa中的基础数据同步到workflow
	 */
	@Scheduled(cron = "0 0 1 * * ?")
	public void saveWorkflowJob() {
		logger.info("Workflow数据抽取任务开始......");
		StopWatch stopWatch = new StopWatch();
		synchroWorkflow.saveData();
		logger.info("Workflow数据抽取任务结束，共耗时：[{}]分", stopWatch.elapsedTime() / 60000);
	}

	@Scheduled(cron = "0 */10 * * * ?")
	public void mq() {
		logger.info("MQ消息补偿任务开始......");
		StopWatch                                      stopWatch = new StopWatch();
		com.eascs.admin.common.vo.ModelResult<Boolean> result    = mqService.mtc();
		if (result.isSuccess()) {
			logger.info("MQ消息补偿任务结束，共耗时：[{}]分", stopWatch.elapsedTime() / 60000);
		} else {
			logger.info("MQ消息补偿任务失败，共耗时：[{}]分。原因：[{}]", stopWatch.elapsedTime() / 60000, result.getErrMsg());
		}

	}
	
}
